package com.zhb.controller;

/**
 * Created by jarvan4dev on 16/5/21.
 */

import com.zhb.entity.ActivityInfo;
import com.zhb.entity.Loan;
import com.zhb.entity.MyActivity;
import com.zhb.entity.UserInfo;
import com.zhb.services.ActivityService;
import com.zhb.services.LoanService;
import com.zhb.services.UserService;
import com.zhb.util.IOUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.util.List;

/**
 * 上下文controller, 托管httpClient
 * 保持httpClient单例, 即可保证当前登录态
 */
public class OpacContextController {

    private HttpClient httpClient;

    private UserService userService;
    private LoanService loanService;

    private OpacContextController(){
        httpClient = HttpClients.createDefault();
        userService = new UserService(httpClient);
        loanService = new LoanService(httpClient);
    }

    private void doLogin(String username, String password) throws IOException {
        // 登录
        userService.doLoginOpac(username, password);
    }

    private void saveLoanList() throws IOException {
        String html = loanService.visitLoanPage();
        List<Loan> loanList = loanService.getMyLoan(html);
        IOUtils.saveResult(loanList, "myLoanList.json");
    }

    public static void main(String[] args) throws IOException {
        OpacContextController controller = new OpacContextController();
        String username = "0004989";//IOUtils.getInput("请输入用户名");
        String password = "19801025";//IOUtils.getInput("请输入密码");
        controller.doLogin(username, password);
        controller.saveLoanList();
    }
}
